<?php

class Translator {

	protected $data = null;
	
	public function __construct($dictionary) {
		$this->data = sfYaml::load(DATA_DIR . "/$dictionary.yml");
	}
	
	public function translate($source) {
		$temp = trim(preg_replace("~[ \t]+~", " ", $source));
		
		if(isset($this->data['replacements']['phrase']) && count($this->data['replacements']['phrase'])) {
			foreach($this->data['replacements']['phrase'] as $phrase => $replacement) {
				$temp = str_ireplace($phrase, $replacement, $temp);
			}
		}
		
		if(isset($this->data['replacements']['midword']) && count($this->data['replacements']['midword'])) {
			foreach($this->data['replacements']['midword'] as $fragment => $replacement) {
				$temp = preg_replace("~\\B$fragment\\B~i", $replacement, $temp);
			}
		}
		
		if(isset($this->data['replacements']['suffix']) && count($this->data['replacements']['suffix'])) {
			foreach($this->data['replacements']['suffix'] as $suffix => $replacement) {
				$temp = preg_replace("~$suffix\\b~i", $replacement, $temp);
			}
		}
		
		if(isset($this->data['replacements']['word']) && count($this->data['replacements']['word'])) {
			foreach($this->data['replacements']['word'] as $word => $replacement) {
				$temp = preg_replace("~\\b$word\\b~i", $replacement, $temp);
			}
		}
		if($this->data['options']['uppercase'])
			$temp = strtoupper($temp);
		return $temp;
	}

}